#!/usr/bin/env bash

LANG=C

bins=('ping' 'parallel' 'date' 'sed' 'grep' 'cut')
for bin in "${bins[@]}"; do
    if [[ -x "/usr/bin/${bin}" ]]; then
        continue
    elif [[ ! -x "/usr/bin/${bin}" ]]; then
        [[ ! -x "/usr/bin/pacman" ]] && echo "Please install /usr/bin/${bin}" && exit 1
        echo -e "Please install ${bin}\nIt must be the package: $(pacman -Fq /usr/bin/${bin} 2>/dev/null)"
        exit 1
    fi
done

GATEWAY="$(ip route | grep "default" | sed -r 's/default via ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) .+/\1/g')"
INTERNET="ping.manjaro.org"
COUNT=5

while true; do

    function pingpong ()
    {
        parallel --keep-order --jobs 2 --ungroup ping -c $COUNT ::: $GATEWAY $INTERNET | grep 'loss' | cut -d ' ' -f6
    }

    LOSS=( $(pingpong) )
    wait
    CURRENT="$(date '+%Y-%m-%d %H:%M:%S')"

    [[ "${LOSS[0]}" != "0%" ]] && echo "[${CURRENT}] ($GATEWAY $COUNT) $msg" 2>/dev/null | tee -a ping.log
    [[ "${LOSS[0]}" == "0%" ]] && echo "[${CURRENT}] ($GATEWAY $COUNT) No packets lost."
    [[ "${LOSS[1]}" != "0%" ]] && echo "[${CURRENT}] ($INTERNET $COUNT) $msg" 2>/dev/null | tee -a ping.log
    [[ "${LOSS[1]}" == "0%" ]] && echo "[${CURRENT}] ($INTERNET $COUNT) No packets lost."

done